package com.regan.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.google.common.collect.Lists;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.regan.erp.aop.NotNullPara;
import com.regan.erp.aop.UserLoginInterceptor;
import com.regan.erp.api.aop.ApiIgnore;
import com.regan.erp.controller.base.BaseController;
import com.regan.erp.exception.MyBaselogicException;
import com.regan.erp.model.Area;
import com.regan.erp.model.ResultPoJo;
import com.regan.erp.model.User;
import com.regan.erp.util.UserUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础管理
 * @action /
 * @author zhangby
 * @date 2018/9/12 下午4:45
 */
public class IndexController extends BaseController {

    /**
     * 跳转首页
     */
    @Clear
    @ApiIgnore
    public void index() {
        render("/index.html");
    }

    /**
     * 验证是否登录超时
     * @title 超时验证
     * @resqParam flag|超时验证 <br> true:登录未超时，false:登录超时|boolean|非必填
     * @respBody {"code":"000","data":{"flag":true},"msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/9/11 下午6:12
     */
    @Clear(UserLoginInterceptor.class)
    public void verify() {
        //验证用户登录是否超时，从session里获取登录用户信息
        renderJson(ResultPoJo.create().result(Kv.create().set("flag",UserUtil.verify(getSession()))));
    }

    /**
     * 用户登录功
     * @title 登录接口
     * @param username|用户名|string|必填
     * @param password|密码|string|必填
     * @resqParam code|状态码 000:登录成功 ，否则失败|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post,get
     * @author zhangby
     * @date 2018/9/11 下午6:12
     */
    @NotNullPara({"username","password"})
    @Clear(UserLoginInterceptor.class)
    public void login() {
        //获取登录用户名密码
        String username = getPara("username");
        String password = getPara("password");
        //验证登录用户
        User user = User.dao.findFirst("SELECT id,password,login_flag from t_user where user_name=?", username);
        if (ObjectUtil.isNull(user)) {
            //用户不存在
            throw new MyBaselogicException("103");
        } else if(ObjectUtil.equal(User.LOGIN_FLAG_1,user.getLoginFlag())){
            //账号已禁用
            throw new MyBaselogicException("105");
        } else {
            //验证密码是否正确
            String md5Pwd = SecureUtil.md5(password);
            if (md5Pwd.equals(user.getPassword())) {
                //登录成功
                UserUtil.addSession(user,getSession());
            } else {
                //登录失败,密码错误
                throw new MyBaselogicException("104");
            }
        }
        renderJson(ResultPoJo.create());
    }

    /**
     *
     * 退出登录接口
     * @title 退出登录
     * @resqParam code|状态码|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/9/12 下午2:49
     */
    public void logout() {
        logout4Session();
        renderJson(ResultPoJo.create());
    }

    /**
     *
     * 省市县三级联动
     * @title 地区查询
     * @param parentId|省市县父类id，默认（省）：-1|String|非必填
     * @resqParam value|地区Id|String|必填
     * @resqParam label|地区名|String|必填
     * @respBody {"code":"000","data":{ "areaId":1,"areaName":"北京市" },"msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/9/13 下午3:03
     */
    public void area() {
        List<Area> areas = Area.dao.find("select areaId value,areaName label from t_area where parentId=?", getPara("parentId","-1"));
        renderJson(ResultPoJo.create().result(areas));
    }

    /**
     *
     * 图片上传
     * @title 上传头像
     * @param head_img|头像地址|String|必填
     * @resqParam code|用户名|String|必填
     * @resqParam data|数据|object|非必填
     * @resqParam msg|消息信息|String|必填
     * @respBody {"code":"000","data":"","msg":"success"}
     * @requestType post
     * @author zhangby
     * @date 2018/9/14 下午3:42
     */
    public void uploadImg() throws IOException {
        User user = UserUtil.getUser(getSession());
        new User().setId(user.getId()).setHeadImg(getPara("head_img")).update();
        renderJson(ResultPoJo.create());
    }

}
